import { extendApi } from '@inbiz/utils';

export default extendApi(
  (_events: any, selectValue: any, option: any, $option: any, props: any, options: any[]) => {
    return {
      getSaveData: () => {
        let propsValue = props.value;
        if (props.storageConfig && props.childModelConfig) {
          const newOptioins = options.map((item) => {
            return {
              ...item,
              text: item.label,
            };
          });
          let dataList: any[] = [];
          if (typeof propsValue == 'string' && propsValue !== '') {
            propsValue = propsValue.split(',');
            propsValue = propsValue.map((item: string) => item.trim());
          }
          if (Array.isArray(propsValue)) {
            dataList = propsValue.map((item: string) => {
              let fil = newOptioins.filter((val) => val.value === item);
              if (!fil.length) {
                return {}
              }
              return props.storageConfig?.tableData.reduce((pre: any, next: any) => {
                pre[next.modelAttr] = fil[0][next.componentAttr];
                pre['_opt'] = 'add';
                return pre;
              }, {});
            });
          }
          const data = {
            value: Array.isArray(propsValue) ? propsValue.join(',') : propsValue,
            childData: props.storageConfig
              ? {
                  controlId: props.controlId,
                  modelKey: props.storageConfig?.business,
                  datas: [
                    ...dataList,
                    {
                      _opt: 'del',
                    },
                  ],
                }
              : undefined,
          };
          return data;
        }
        return {
          value: Array.isArray(propsValue) ? propsValue.join(',') : propsValue,
        };
      },
      loadData: (list: { value: string; label: string }[]) => {
        $option(list);
      },
      getData: () => {
        return option;
      },
      getText: () => {
        if (Array.isArray(selectValue)) {
          return option
            ?.filter((item: any) => selectValue.includes(item.value))
            .map((item: any) => item.label)
            .join(',');
        } else {
          return option?.filter((item: any) => item.value === selectValue)[0]?.label;
        }
      },
      // 目前只支持设置单个选中，后续根据需求调整
      setValueToLabel: (label: string) => {
        if (!label || label == '' || !label?.length) return;
        let option: {label?: string; value?: string} | undefined = undefined;
          // 完全匹配查找
        option = options.find(option => option.label === label);
        // 没有完全匹配则进行模糊匹配查找
        if (!option) {
          option = options.find(option => option.label.includes(label));
        }
        if (option) {
          if (props.selectMode) {
            props.onChange && props.onChange([option?.value]);
          } else {
            props.onChange && props.onChange(option?.value);
          }
          return option;
        }
        return;
      }
    };
  },
);
